package com.hankol.winecellar.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baselibrary.MessageBus
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.EventBusUtils
import com.hankol.winecellar.bean.BannerConfigListBean
import com.hankol.winecellar.bean.SeckillAddSubscribeBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class BaseRequestManager {
    companion object {
        @Volatile
        private var instance: BaseRequestManager? = null

        fun getInstance(): BaseRequestManager {
            return instance ?: synchronized(this) {
                instance ?: BaseRequestManager().also { instance = it }
            }
        }
    }

    /**秒杀--添加预约
     * @author: merbng
     * @time: 2023/3/20 13:40
     * @description:
     */
    fun reqSeckillAddSubscribe(listData: MutableLiveData<SeckillAddSubscribeBean>, actId: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqSeckillAddSubscribe(actId)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<SeckillAddSubscribeBean>(this) {
            override fun onSuccess(t: SeckillAddSubscribeBean, json: String) {
                EventBusUtils.sendNotify(MessageBus.msgId_refresh_seckill_sub_status)
                if (t != null) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**秒杀--取消预约
     * @author: merbng
     * @time: 2023/3/20 13:41
     * @description:
     */
    fun reqSeckillCancelSubscribe(mutableLiveData: MutableLiveData<Any>, actId: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqSeckillCancelSubscribe(actId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                EventBusUtils.sendNotify(MessageBus.msgId_refresh_seckill_sub_status)
                mutableLiveData.value = t
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                mutableLiveData.value = false
            }
        })
    }

    /**根据类型获取banner列表（首页大改版）
     * @time: 2023/4/20 11:48
     * @description:    bannerType： banner类型 0首页banner 1商城banner 2附近banner 3中心banner 4话题轮播图
     */
    fun reqGetBannerConfigList(bannerType: Int, otherId: Long = -1, mutableLiveData: MutableLiveData<BannerConfigListBean>) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqGetBannerConfigList(bannerType, otherId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<BannerConfigListBean>(this) {
            override fun onSuccess(t: BannerConfigListBean, json: String) {
                mutableLiveData.value = t
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                mutableLiveData.value = null
            }
        })
    }

}